package com.gitee.elf.core.data

import com.google.gson.*
import org.bukkit.inventory.ItemStack
import taboolib.common5.util.decodeBase64
import taboolib.common5.util.encodeBase64
import taboolib.platform.util.deserializeToItemStack
import taboolib.platform.util.serializeToByteArray
import java.lang.reflect.Type

class DataItemStack(val value: ItemStack) : Data {

    override fun getValue(): Any {
        return value
    }
    override fun clone(): Data {
        return DataItemStack(value.clone())
    }

    @SerializerType(ItemStack::class)
    class Serializer : JsonSerializer<DataItemStack>, JsonDeserializer<DataItemStack> {

        override fun serialize(src: DataItemStack?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            return JsonPrimitive(src!!.value.serializeToByteArray(true).encodeBase64())
        }

        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): DataItemStack {
            return DataItemStack(json!!.asString.decodeBase64().deserializeToItemStack(true))
        }

    }

}